﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Robotics.Race
{
    public class TerrainRule : Rule
    {
        public int RoadValue { get; set; }
        public int RoadRatio { get; set; }

        public int GrassValue { get; set; }
        public int GrassRatio { get; set; }

        public int TerrainBoundary { get { return (GrassValue + RoadValue) / 2; } }

        public override void Apply(Car car)
        {
            int light = 0;
            if (car.Environment.LeftLight.HasValue && car.Environment.RightLight.HasValue)
                light = (car.Environment.LeftLight.Value + car.Environment.RightLight.Value) / 2;
            else if (car.Environment.LeftLight.HasValue)
                light = car.Environment.LeftLight.Value;
            else if (car.Environment.RightLight.HasValue)
                light = car.Environment.RightLight.Value;
            if (light < TerrainBoundary)
            {
                // The car is on grass
                car.ControlGear.Acceleration *= (double)GrassRatio / 100;
                car.ControlGear.Steering *= (double)GrassRatio / 100;
                if (car.Speed > GrassRatio)
                    car.ControlGear.Acceleration = -1.0;
            }
            else
            {
                // The car is on road
                car.ControlGear.Acceleration *= (double)RoadRatio / 100;
                car.ControlGear.Steering *= (double)RoadRatio / 100;
                if (car.Speed > RoadRatio)
                    car.ControlGear.Acceleration = -1.0;
            }
        }
    }
}
